
"use strict";

angular.module('app.user').controller('AllUserCtrl', function ($scope,userHttpS,$cookieStore,$http) {
    var comp_id = $cookieStore.get('comp_id');
    var pagefunction = function () {
        run_jqgrid_function();
        function run_jqgrid_function() {
            jQuery("#jqgrid").jqGrid({
                url: baseurls + "user/getalluser",
              
                datatype: "json",
                height: 'auto',
                colNames: ['电子邮件', 'userid','用户名', '角色','部门','公司ID'],
                colModel: [
                    { name: 'email', index: 'email', align: "left", editrules: { email: true, required: true }, editable: true},
                    { name:'user_id', index: 'user_id', editable:true, hidden:true},
					{ name: 'name', index: 'name', editrules: { required: true }, editable: true },
					{
                        name: 'role_name', index: 'role_name', editable: true, 
                        edittype: "select",
					    editoptions: { value: myAppGlobal.userManager.allroles }
                    },
					{
                        name: 'dept_name', index: 'dept_name', editable: true,
                        edittype: "select",
					    editoptions: { value: myAppGlobal.userManager.alldepts }
                    },
                    {
                        name: 'company_id', index: 'company_id', editable:true, 
                        formatter:function(){return comp_id},
                        // hidden:true
                    }
                ],
                rowNum: 10,
                rowList: [10, 20, 30],
                pager: '#pjqgrid',
                sortname: 'email',
                toolbarfilter: true,
                viewrecords: true,
                sortorder: "asc",

                loadBeforeSend: function (xhr) {
                    xhr.setRequestHeader("token", custom.token);
                    return xhr;
                },
              
                // gridComplete: function(){
                // 	var ids = jQuery("#jqgrid").jqGrid('getDataIDs');
                // 	for(var i=0;i < ids.length;i++){
                // 		var cl = ids[i];
                // 		be = "<button class='btn btn-xs btn-default' data-original-title='Edit Row' onclick=\"jQuery('#jqgrid').editRow('"+cl+"');\"><i class='fa fa-pencil'></i></button>";
                // 		se = "<button class='btn btn-xs btn-default' data-original-title='Save Row' onclick=\"jQuery('#jqgrid').saveRow('"+cl+"');\"><i class='fa fa-save'></i></button>";
                // 		ca = "<button class='btn btn-xs btn-default' data-original-title='Cancel' onclick=\"jQuery('#jqgrid').restoreRow('"+cl+"');\"><i class='fa fa-ban'></i></button>";
                // 		//ce = "<button class='btn btn-xs btn-default' onclick=\"jQuery('#jqgrid').restoreRow('"+cl+"');\"><i class='fa fa-times'></i></button>";
                // 		//jQuery("#jqgrid").jqGrid('setRowData',ids[i],{act:be+se+ce});
                // 		jQuery("#jqgrid").jqGrid('setRowData',ids[i],{act:be+se+ca});
                // 	}
                // },
                editurl: baseurls + "user/edituser",
                caption: "用户信息表",
                multiselect: true,
                autowidth: true,

            });
            jQuery("#jqgrid").jqGrid('navGrid', "#pjqgrid", {
                edit: false,
                add: false,
                del: true
            });
            jQuery("#jqgrid").jqGrid('inlineNav', "#pjqgrid" , {

                // //  edittext: "编辑",
                // edittitle:"编辑", 
                // // add: false,
                // // savetext: "保存",
                // savetitle:"保存",
                // //  canceltext: "取消",
                // canceltitle:"取消",
                // addParams: { position: "afterSelected" }
            });
            /* Add tooltips */
            $('.navtable .ui-pg-button').tooltip({
                container: 'body'
            });

            jQuery("#m1").click(function () {
                var s;
                s = jQuery("#jqgrid").jqGrid('getGridParam', 'selarrrow');
                alert(s);
            });
            jQuery("#m1s").click(function () {
                jQuery("#jqgrid").jqGrid('setSelection', "13");
            });

            // remove classes
            $(".ui-jqgrid").removeClass("ui-widget ui-widget-content");
            $(".ui-jqgrid-view").children().removeClass("ui-widget-header ui-state-default");
            $(".ui-jqgrid-labels, .ui-search-toolbar").children().removeClass("ui-state-default ui-th-column ui-th-ltr");
            $(".ui-jqgrid-pager").removeClass("ui-state-default");
            $(".ui-jqgrid").removeClass("ui-widget-content");

            // add classes
            $(".ui-jqgrid-htable").addClass("table table-bordered table-hover");
            $(".ui-jqgrid-btable").addClass("table table-bordered table-striped");


            $(".ui-pg-div").removeClass().addClass("btn btn-sm btn-primary");
            $(".ui-icon.ui-icon-plus").removeClass().addClass("fa fa-plus");
            $(".ui-icon.ui-icon-pencil").removeClass().addClass("fa fa-pencil");
            $(".ui-icon.ui-icon-trash").removeClass().addClass("fa fa-trash-o");
            $(".ui-icon.ui-icon-search").removeClass().addClass("fa fa-search");
            $(".ui-icon.ui-icon-refresh").removeClass().addClass("fa fa-refresh");
            $(".ui-icon.ui-icon-disk").removeClass().addClass("fa fa-save").parent(".btn-primary").removeClass("btn-primary").addClass("btn-success");
            $(".ui-icon.ui-icon-cancel").removeClass().addClass("fa fa-times").parent(".btn-primary").removeClass("btn-primary").addClass("btn-danger");

            $(".ui-icon.ui-icon-seek-prev").wrap("<div class='btn btn-sm btn-default'></div>");
            $(".ui-icon.ui-icon-seek-prev").removeClass().addClass("fa fa-backward");

            $(".ui-icon.ui-icon-seek-first").wrap("<div class='btn btn-sm btn-default'></div>");
            $(".ui-icon.ui-icon-seek-first").removeClass().addClass("fa fa-fast-backward");

            $(".ui-icon.ui-icon-seek-next").wrap("<div class='btn btn-sm btn-default'></div>");
            $(".ui-icon.ui-icon-seek-next").removeClass().addClass("fa fa-forward");

            $(".ui-icon.ui-icon-seek-end").wrap("<div class='btn btn-sm btn-default'></div>");
            $(".ui-icon.ui-icon-seek-end").removeClass().addClass("fa fa-fast-forward");

            // update buttons

            $(window).on('resize.jqGrid', function () {
                jQuery("#jqgrid").jqGrid('setGridWidth', $("#content").width()-20);
            })


        } // end function


    };

    var str = "";
    myAppGlobal.ajaxFunction.ax(
          "user/getalldeparts",
           null,
           null,
           null,
           null,
           function (data) {


               for (var i = 0; i < data.length; i++) {
                   if (i != data.length - 1) {
                       str += data[i].id + ":" + data[i].name + ";";
                   } else {
                       str += data[i].id + ":" + data[i].name;
                   }
               }
               
               myAppGlobal.userManager.alldepts = str;
               allroles();
              console.log(myAppGlobal.userManager.alldepts);

              // pagefunction();

           },
           function (data) {


           });

 
    var allroles = function(){
    
        myAppGlobal.userManager.allroles = "";
        myAppGlobal.ajaxFunction.ax(
              "permission/getallrole",
               null,
               null,
               null,
               null,
               function (data) {

                   var len = data.length;
                   for (var i = 0; i < len; i++) {
                       if (i != len - 1) {
                           myAppGlobal.userManager.allroles += data[i].id + ":" + data[i].name + ";";
                           
                       } else {
                           myAppGlobal.userManager.allroles += data[i].id + ":" + data[i].name;
                       }
                    }
                    
                    console.log(myAppGlobal.userManager.allroles);

                   pagefunction();

               },
               function (data) {


               });


    }  
 

  
});